package com.lyw.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lyw.forum.pojo.dataobject.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    /**
     * 查询所有帖子 不包含content
     * @return 帖子列表
     */
    @Select({
            "SELECT",
            "    u.id AS u_id,",
            "    u.avatar_url AS u_avatar_url,",
            "    u.nickname AS u_nickname,",
            "    a.id,",
            "    a.board_id,",
            "    a.user_id,",
            "    a.title,",
            "    a.visit_count,",
            "    a.reply_count,",
            "    a.like_count,",
            "    a.state,",
            "    a.create_time,",
            "    a.update_time",
            "FROM t_article a, t_user u",
            "WHERE a.user_id = u.id",
            "    AND a.delete_state = 0",
            "ORDER BY a.create_time DESC",
            "LIMIT 100"
    })
    @Results({
            // 用户对象字段映射 (使用完整路径)
            @Result(property = "user.id", column = "u_id"),
            @Result(property = "user.avatarUrl", column = "u_avatar_url"),
            @Result(property = "user.nickname", column = "u_nickname")
    })
    List<Article> selectAll();


    /**
     * 根据板块id查询所有帖子 不包含content
     * @param id 板块id
     * @return
     */
    @Select({
            "SELECT",
            "    u.id AS u_id,",
            "    u.avatar_url AS u_avatar_url,",
            "    u.nickname AS u_nickname,",
            "    a.id,",
            "    a.board_id,",
            "    a.user_id,",
            "    a.title,",
            "    a.visit_count,",
            "    a.reply_count,",
            "    a.like_count,",
            "    a.state,",
            "    a.create_time,",
            "    a.update_time",
            "FROM t_article a, t_user u",
            "WHERE a.user_id = u.id",
            "    AND a.delete_state = 0",
            "    AND a.board_id = #{id}",
            "ORDER BY a.create_time DESC",
            "LIMIT 100"
    })
    @Results({
            // 用户对象字段映射 (使用完整路径)
            @Result(property = "user.id", column = "u_id"),
            @Result(property = "user.avatarUrl", column = "u_avatar_url"),
            @Result(property = "user.nickname", column = "u_nickname")
    })
    List<Article> selectAllByBoardId(Integer id);

    /**
     * 查询帖子详情
     * @param id 帖子id
     * @return 帖子详情
     */
    @Select({
            "SELECT",
            "    u.id AS u_id,",
            "    u.avatar_url AS u_avatar_url,",
            "    u.nickname AS u_nickname,",
            "    u.gender AS u_gender,",
            "    u.is_admin AS u_is_admin,",
            "    u.state AS u_state,",
            "    b.id AS b_id,",
            "    b.name AS b_name,",
            "    b.state AS b_state,",
            "    a.id,",
            "    a.board_id,",
            "    a.user_id,",
            "    a.title,",
            "    a.content,",
            "    a.visit_count,",
            "    a.reply_count,",
            "    a.like_count,",
            "    a.state,",
            "    a.create_time,",
            "    a.update_time",
            "FROM t_article a, t_user u, t_board b",
            "WHERE a.user_id = u.id",
            "    AND a.board_id = b.id",
            "    AND a.delete_state = 0",
            "    AND a.id = #{id}",
    })
    @Results({
            // 用户对象字段映射 (使用完整路径)
            @Result(property = "user.id", column = "u_id"),
            @Result(property = "user.avatarUrl", column = "u_avatar_url"),
            @Result(property = "user.nickname", column = "u_nickname"),
            @Result(property = "user.gender", column = "u_gender"),
            @Result(property = "user.isAdmin", column = "u_is_admin"),
            @Result(property = "user.state", column = "u_state"),

            // 板块对象字段映射 (使用完整路径)
            @Result(property = "board.id", column = "b_id"),
            @Result(property = "board.name", column = "b_name"),
            @Result(property = "board.state", column = "b_state"),
    })
    Article selectDetailById(Integer id);

    /**
     * 根据用户id查询帖子
     * @param id
     * @return
     */
    @Select({
            "SELECT",
            "    b.id AS b_id,",
            "    b.name AS b_name,",
            "    a.id,",
            "    a.board_id,",
            "    a.user_id,",
            "    a.title,",
            "    a.visit_count,",
            "    a.reply_count,",
            "    a.like_count,",
            "    a.state,",
            "    a.create_time,",
            "    a.update_time",
            "FROM t_article a, t_board b",
            "WHERE a.board_id = b.id",
            "    AND a.delete_state = 0",
            "    AND a.user_id = #{id}",
            "ORDER BY a.create_time DESC"
    })
    List<Article> selectByUserId(Integer id);
}
